package com.example.travel_front.Api;

import com.example.travel_front.APP;
import com.example.travel_front.Model.Data;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.List;

public class JsonData {
    private static List<Data> datas = null;

    public static List<Data> getData() {
        initData();
        return datas;
    }

    public static void initData(){
        if (datas == null) {
            try {
                InputStream is = APP.context.getAssets().open("data.json");
                int size = is.available();
                byte[] buffer = new byte[size];
                is.read(buffer);
                is.close();
                String json = new String(buffer, "UTF-8");
                // 处理 json 字符串...

                Gson gson = new Gson();
                // 创建Type对象，用于指定List的元素类型
                Type listType = new TypeToken<List<Data>>() {
                }.getType();
                // 将JSON字符串转换为List
                datas = gson.fromJson(json, listType);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}
